#include<iostream>
#include<string>
#include<vector>
#include<boost/filesystem.hpp>
#include"util.hpp"
#define DEVE '\3'
using namespace std;
const string path="data/input/";
const string raw="data/clear/raw.txt";
struct dishtml
{
string title;
string body;
string url;
};
bool getpath(vector<string>&filepath,const string&path);
bool cfile(vector<dishtml>&clearpath,vector<string>&filepath);
bool tofile(const string&raw,vector<dishtml>&clearpath);
int x; int y;
int h;
int main()
{
vector<string> filepath;
if(!getpath(filepath,path))
{
    cout<<"get path error"<<endl;
    return 1;
}
vector<dishtml> clearpath;
if(!cfile(clearpath,filepath))
{
    cout<<"clear path error"<<endl;
    return 2;
}
if(!tofile(raw,clearpath))
{
    cout<<"to path error"<<endl;
    return 3;
}
return 0;
}
bool getpath(vector<string>&filepath,const string&mypath)
{
    namespace bst=boost::filesystem;
    bst::path root_path(mypath);
    if(!bst::exists(root_path))
    {
        cout<<"path disappear"<<endl;
        return false;
    }
  bst::recursive_directory_iterator end;
  for( bst::recursive_directory_iterator start(root_path);start!=end;start++)
  {
if(!bst::is_regular_file(*start))
{
    continue;
}
if(start->path().extension()!=".html")
{
    continue;
}

filepath.push_back(start->path().string());
  }
return true;
}
bool getTite(dishtml&an,string&text)
{
    string tit="<title>";
    string endtit="</title>";
size_t start=text.find("<title>");
if(start==string::npos)
{
    cout<<"gettite"<<endl;
    return false;
}
start+=tit.size();
size_t end;
end=text.find("</title>");
if(end==string::npos)
{
    cout<<"titttt"<<endl;
    return false;
}
an.title=text.substr(start,end-start);
return true;
}
bool getBody(dishtml&an,string&text)
{
enum en
{
    count,
    body
};
int i=0;
enum en statu=count;
string thebody;
while(i<text.size())
{
switch(statu)
{
case count:
if(text[i]=='>')
statu=body;
break;
case body:
if(text[i]=='<')
statu=count;
else 
{
    if(text[i]=='\n')
    text[i]=' ';
thebody.push_back(text[i]);
}
}
i++;
}
an.body=thebody;
return true;
}
bool geturl(dishtml&an,string&file_path)
{
string head="https://www.boost.org/doc/libs/1_85_0/doc/html/";
string teil=file_path.erase(0,path.size());
string url=head+teil;
an.url=url;
return true;
}
bool cfile(vector<dishtml>&clearpath,vector<string>&filepath)
{
for(auto e:filepath)
{
    string text;
    if(!util::fileutil::readfile(e,text))
    {
        continue;
    }
    dishtml an;
    if(!getTite(an,text))
    {
     
        continue;
    }
    if(!getBody(an,text))
    {
        cout<<"getbody"<<endl;

        continue;
    }
    if(!geturl(an,e))
    {
        cout<<"geturl"<<endl;

        continue;
    }

    clearpath.push_back(move(an));
}
return true;
}
bool tofile(const string&raw,vector<dishtml>&clearbody)
{
    ofstream out(raw,ios::out|ios::binary);
    if(!out.is_open())
    {
        cout<<"open error"<<endl;
        return false;
    }
    for(auto&e:clearbody)
    {
string outto=e.title;
outto+=DEVE;
outto+=e.body;
outto+=DEVE;
outto+=e.url;
outto+='\n';
out.write(outto.c_str(),outto.size());
    }
    return true;
}